package org.fixies.spreadsheet.excel;

import org.fixies.spreadsheet.AbstractBookLoader;
import org.fixies.spreadsheet.Book;
import org.fixies.spreadsheet.interpreter.BookContext;
import org.fixies.spreadsheet.interpreter.BookNode;
import org.fixies.utils.UnexpectedProblemException;


public class XlsBookLoader extends AbstractBookLoader {


    public XlsBookLoader(Class<?> clazz, BookContext bookContext) {
        super(clazz, bookContext);
    }

    public XlsBookLoader(Class<?> clazz, String fName, BookContext bookContext) {
        super(clazz, fName, bookContext);
    }


    @Override
    protected Book loadBook(String bookPath) {
        try {
            return doLoad(bookPath);
        } catch (RuntimeException e) {
            throw new UnexpectedProblemException(e, "loadBook failed. path=[", bookPath, "]");
        }
    }
    
    private Book doLoad(String bookPath) {
        BookContext bookContext = getBookContext();
        bookContext.load(bookPath, getResourceLoader());
        BookNode bookNode = new BookNode(bookPath);
        bookNode.evaluate(bookContext);
        return bookNode.getResult();
    }
}
